/**
 * Created by Administrator on 2018/10/17 0017.
 */

function getOtherCmListPage(pageurl,keyword,type,curr){
    $("#allChk").prop("checked",false);
    sign.params={
        keyword:keyword,
        curr:curr
    };
    var encode_post = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(pageurl,encode_post, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);
            //选择要替换的内容
            otherCm_list(datass);

            form.render('checkbox');
            layer.close(index);
            if(datass.data.pages<=1){
                $('#pages').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'pages', 			// 容器
                    count: datass.data.total,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            getOtherCmListPage(pageurl,keyword,1, obj.curr);
                        }
                    }
                });
            }
        });
    });
}
//我的文章评论列表
function otherCm_list(data) {
    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#page_list').html(html);
    }
    else {
        list = data['data']['list'];
        for (var i in list) {
            list[i].id = list[i].id ? list[i].id : '';
            list[i].compose_id = list[i].compose_id ? list[i].compose_id : '';
            list[i].title = list[i].title ? list[i].title : '';
            list[i].content = list[i].content ? list[i].content : '';
            list[i].createtime = list[i].createtime ? list[i].createtime : '';
            list[i].username = list[i].username ? list[i].username : '';

            html += '<tr> <td class="tc"><input name="id[]" value="' + list[i].id + '" type="checkbox"></td> <td>';
            html += '<a target="_blank" href="#" style="color: #00b3ee" onclick="articleInfo(' + list[i].compose_id + ')" title="' + list[i].title + '">' + list[i].title + '</a></td>';
            html += '<td>' + list[i].username + '评论说：' + list[i].content + '</td>';
            html += '<td>' + list[i].createtime + '</td>';
            html += '<td class="link-update">';

            html += '<a class="link-del" href="#" onclick="del_comment(' + list[i].id + ',url_list.cm_del,1)">删除</a>';
            html += '</td></tr>';
            childList = list[i].reply;
            for (var j in childList){
                childList[j].id =childList[j].id?childList[j].id:'';
                childList[j].content =childList[j].content?childList[j].content:'';
                childList[j].createtime=childList[j].createtime?childList[j].createtime:'';
                childList[j].from_name =childList[j].from_name?childList[j].from_name:'';
                childList[j].to_name =childList[j].to_name?childList[j].to_name:'';

                html += '<tr> <td class="tc"><input name="id[]" value="' + childList[j].id + '" type="checkbox"></td> <td>';
                html += '<a target="_blank" href="#" style="color: #00b3ee" onclick="articleInfo(' + list[i].compose_id + ')" title="' + list[i].title + '">' + list[i].title + '</a></td>';
                html += '<td>' + childList[j].from_name + '回复'+childList[j].to_name+'说：' + childList[j].content + '</td>';
                html += '<td>' + list[j].createtime + '</td>';
                html += '<td class="link-update">';

                html += '<a class="link-del" href="#" onclick="del_comment(' + list[i].id + ',url_list.cm_del,1)">删除</a>';
                html += '</td></tr>';

            }
        }
        $('#page_list').html(html);
    }
}



//删除
function del_comment(id,url,type) {
    var id = id;
    sign.params={
        id:id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url,encode_post, function(data){
        if(typeof data == 'string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        if(data.code==200){
            layer.msg(data.msg,{icon:1},1500);
            if (type==1) {
                getCommentListPage(url_list.comment_list, keyword, type);
            }else if (type=='2') {
                getCommentListPage(url_list.myComment_list, keyword, type);
            }else if (type=='3'){
                getCommentListPage(url_list.myReply_list,keyword,type);
            }
        }else {
            layer.msg(data.msg,{icon:2},1500);
        }
    });
}
//批量删除
function dels_comment(type) {
    if(type==1 || type==2){
        var url=url_list.cm_del;
    }else {
        url = url_list.reply_del;
    }
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    if (confirm("确定要删除？")) {
        var checkedList = new Array();

        $("input[name='id[]']:checked").each(function () {
            checkedList.push($(this).val());
        });
    }
    var check_id =checkedList.join(',');

    sign.params={
        id :check_id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url,encode_post,function (data) {
        if(typeof data=='string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        if (data.code==200){
            layer.msg(data.msg,{icon:1},1500);
            switch (type){
                case '1':getCommentListPage(url_list.comment_list,keyword,1);break;
                case '2':getCommentListPage(url_list.myComment_list,keyword,2);break;
                case '3':getCommentListPage(url_list.myReply_list,keyword,3);break;
                default:break;
            }
        }else{
            layer.msg(data.msg,{icon:2},1500)
        }
    })

}

//全选
$("#allChk").click(function() {
    $("input[name='id[]']").prop("checked",this.checked);
});

